package com.zoroki.hmusic.controller;


import com.zoroki.hmusic.constant.MessageConstant;
import com.zoroki.hmusic.entity.Playlist;
import com.zoroki.hmusic.entity.PlaylistSong;
import com.zoroki.hmusic.entity.Result;
import com.zoroki.hmusic.entity.User;
import com.zoroki.hmusic.service.IPlaylistService;
import com.zoroki.hmusic.service.IPlaylistSongService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zoroki
 * @since 2023-05-10
 */
@RestController
@RequestMapping("/playlist")
@CrossOrigin("*")
@Tag(name = "歌单")
public class PlaylistController {

    @Autowired
    private IPlaylistService playlistService;

    @Autowired
    private IPlaylistSongService playlistSongService;


    @Operation(summary = "根据用户Id查找歌单")
    @GetMapping("/findPlaylistByUserId")
    @ResponseBody
    public Result findPlaylistByUserId(@RequestParam int userId) {
        List<Playlist> playlists = playlistService.findPlaylistByUserId(userId);
        if (playlists.size() > 0) {
            return new Result(true, MessageConstant.YES, playlists);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据歌单Id和用户Id查找歌单")
    @GetMapping("/findPlaylistByIdAndUserId")
    @ResponseBody
    public Result findPlaylistByIdAndUserId(@RequestParam int id,@RequestParam int userId) {
        Playlist playlists = playlistService.findPlaylistByIdAndUserId(id,userId);
        if (playlists != null) {
            return new Result(true, MessageConstant.YES, null);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据Id查找歌单")
    @GetMapping("/findPlaylistById")
    @ResponseBody
    public Result findPlaylistById(@RequestParam int id) {
        Playlist playlists = playlistService.findPlaylistById(id);
        if (playlists != null) {
            return new Result(true, MessageConstant.YES, playlists);
        }
        return new Result(false, MessageConstant.NO, null);
    }


    @Operation(summary = "添加歌单到收藏")
    @PostMapping("/addPlaylist")
    @ResponseBody
    public Result addPlaylist(@RequestBody Playlist playlist) {
        int i = playlistService.addPlaylist(playlist);
        if (i > 0) {
            return new Result(true, MessageConstant.PLAYLIST_ADD_SUCCESS, null);
        }
        return new Result(false, MessageConstant.PLAYLIST_ADD_FAILURE, null);
    }

    @Operation(summary = "根据歌单Id删除歌单")
    @DeleteMapping("/deletePlaylistById")
    @ResponseBody
    public Result deletePlaylistById(@RequestParam int id) {
        int i = playlistService.deletePlaylistById(id);
        if (i > 0) {
            return new Result(true, MessageConstant.PLAYLIST_DELETE_SUCCESS, null);
        }
        return new Result(false, MessageConstant.PLAYLIST_DELETE_FAILURE, null);
    }

    @Operation(summary = "根据收藏Id和歌曲Id，将歌曲从收藏中删除")
    @DeleteMapping("/deleteSongFromPlaylist")
    @ResponseBody
    public Result deleteSongFromPlaylist(@RequestParam int playlistId, @RequestParam int songId) {
        int i = playlistSongService.deleteSongFromPlaylist(playlistId, songId);
        if (i > 0) {
            return new Result(true, MessageConstant.PLAYLIST_DELETE_SUCCESS, null);
        }
        return new Result(false, MessageConstant.PLAYLIST_DELETE_SUCCESS, null);
    }

    @Operation(summary = "根据keyword来查找收藏")
    @GetMapping("/findPlaylistByKeyword")
    @ResponseBody
    public Result findPlaylistByKeyword(@RequestParam String keyword){
        SearchHits<Playlist> playlists = playlistService.findPlaylistByKeyword(keyword);
        if (playlists.getTotalHits() > 0) {
            return new Result(true, MessageConstant.PLAYLIST_SEARCH_SUCCESS, playlists);
        }
        return new Result(false, MessageConstant.PLAYLIST_SEARCH_FAILURE, null);
    }

    @Operation(summary = "根据keyword来查找收藏分页")
    @GetMapping("/getPlaylistPageByKeyword")
    @ResponseBody
    public Result getPlaylistPageByKeyword(@RequestParam String keyword,@RequestParam int pageIndex){
        Page<Playlist> playlists = playlistService.getPlaylistPageByKeyword(keyword,pageIndex);
        if (playlists.getTotalElements() > 0) {
            return new Result(true, MessageConstant.PLAYLIST_SEARCH_SUCCESS, playlists);
        }
        return new Result(false, MessageConstant.PLAYLIST_SEARCH_FAILURE, null);
    }
    @Operation(summary = "添加歌曲到收藏")
    @PostMapping("/addPlaylistSong")
    @ResponseBody
    public Result addPlaylistSong(@RequestBody PlaylistSong playlistSong){
        int i = playlistSongService.addPlaylistSong(playlistSong);
        if (i > 0) {
            return new Result(true, MessageConstant.YES, null);
        }
        return new Result(false, MessageConstant.NO, null);
    }
    @Operation(summary = "更新收藏")
    @PutMapping("/updatePlaylist")
    @ResponseBody
    public Result updatePlaylist(@RequestBody Playlist playlist){
        int i = playlistService.updatePlaylist(playlist);
        if (i > 0) {
            return new Result(true, MessageConstant.YES, null);
        }
        return new Result(false, MessageConstant.NO, null);
    }

}
